// pages/register/register.js
var app = getApp();
var common = require("../../utils/Conmon.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    firstStep:false,//true,
    next1: true,//false,
    next2:false,
    num: "",
    telPhone: "",
    ViewPhone: "",
    smsCode: "",
    isClick:true,
    pageUrl: ""
  },
  next1fn: function () {
    this.setData({
      firstStep: false,
      next1: true,
      next2: false
    });
  },
  next2fn: function () {
    var _this = this;
    var phone = _this.data.telPhone;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (phone.length == 0 || phone == '') {
      wx.showModal({
        title: '注册',
        content: '请输入手机号',
        showCancel: false
      });
      return;
    }
    if (!myreg.test(phone)) {
      wx.showModal({
        title: '注册',
        content: '手机号码格式错误',
        showCancel: false
      });
      return;
    }
    if (_this.data.isClick)
    {
      _this.setData({
        isClick: false
        });
       
    //发送短信认证码
    _this.wxSubRequest('GET', app.globalData.apiurl + '/member/api/register/sendauthentication', { custId: app.globalData.custID, phone: phone },
      function (data) {
        if (data.code == 1200) {
          if(data.content=="NO")
          {
            wx.navigateTo({
              url: '/pages/setpass/setpass?telPhone=' + _this.data.telPhone + '&num=' + _this.data.num + '&pageUrl=' + encodeURIComponent(_this.data.pageUrl),
            })
            return false;
          }
          wx.hideLoading()
          _this.setData({
            isClick: true,
            firstStep: false,
            next1: false,
            next2: true,
            ViewPhone: phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
          });
        }
        else {
          
          var content = data.msg;
          if (data.code == 1201) {
            content = "手机号码不能为空!";
          } else if (data.code == 1202) {
            content = "手机号码已存在!";
          }
          wx.hideLoading();
          wx.showModal({
            title: '注册',
            content: content,
            showCancel: false
          });
        }
        _this.setData({
          isClick: true
        });
      },
      function (obj) {
        wx.hideLoading()
        wx.showModal({
          title: '系统',
          content: '请求失败！',
          showCancel: false
        });
        _this.setData({
          isClick: true
        });
      });
  
  
  
    }else
    {
      wx.showModal({
        title: '注册',
        content: '正在发送短信....',
        showCancel: false
      });
      return;
    }},

  /**
   * 短信验证码
   */
  GetCode: function (e) {
    var smsCode = e.detail.value;
    this.setData({
      smsCode: smsCode

    })
  },
  /**
 * 推荐号码
 */
  GetNumber: function (e) {
    var num = e.detail.value;
    this.setData({
      num: num
    })
  },
  /**
 * 手机号码
 */
  GetPhone: function (e) {
    var telPhone = e.detail.value;
    this.setData({
      telPhone: telPhone
    })
  },
  /**
   * 绑定事件（下一步）
   */
  bindClick: function (e) {
    var _this = this;
    if (_this.data.smsCode.length != 4) {
      wx.showModal({
        title: '注册',
        content: '请输入正确验证码',
        showCancel: false
      });
      return;
    }

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(_this.data.telPhone)) {
      wx.showModal({
        title: '注册',
        content: '手机号码格式错误',
        showCancel: false
      });
      return;
    }
    //验证验证码
    _this.wxSubRequest('GET', app.globalData.apiurl + 'member/api/register/verifycode', {
      code: _this.data.smsCode,
      phone: _this.data.telPhone,
      custId: app.globalData.custID
    },
      function (data) {
        if (data.code == 1200) {//成功
          wx.navigateTo({
            url: '/pages/setpass/setpass?telPhone=' + _this.data.telPhone + '&num=' + _this.data.num + '&pageUrl=' + encodeURIComponent(_this.data.pageUrl),
          })
        
        }
        else {
          wx.hideLoading();
          wx.showToast({
            title: data.content,
            duration: 2000
          });
        }
      },
      function (obj) {
        wx.hideLoading()
        wx.showModal({
          title: '系统',
          content: '验证失败！',
          showCancel: false
        });
        return;
      });
  },











  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      pageUrl: decodeURIComponent(options.pageUrl)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  /**
* 微信提交请求获取数据或保存数据
* 
*/
  wxSubRequest: function (method, url, param, callBack, CallBackError) {
    wx.request({
      url: url,
      method: method,
      data: param,
      header: {
        "content-type": "application/json",
        'X_ManuName': app.globalData.manuName
      },
      success: function (res) {
        if (res.statusCode == 200) {
          var data = res.data;
          callBack(data)
          console.log(res.data)
        }
      },
      fail: function (res) {
        CallBackError(res.data)
        console.log(res.data)
      },
      complete: function (res) {
        console.log(res.msg)
      }
    })
  },
})